package com.lee.carinforecord;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

public class DataSave {
    public static Integer availableMile = 0; //剩余里程
    public static Float currentMile = 0.0f; //此次里程
    public static Float currentOil = 0.0f; //此次油耗

    public static SharedPreferences sharedPreferences = null;


    public static void save(Context context) {
        String trips = getTrips(context);
        //Log.i("trips", trips);
        DataSave.setValue(context, "trips", currentMile + "," + currentOil + "|" +trips );

        DataSave.setValue(context, "availableMile", availableMile);
    }

    public static String getTrips(Context context) {
        return (String) DataSave.getValue(context, "trips");
    }
    public static Integer getAvailableMile(Context context) {
        return share(context).getInt("availableMile", 0);
    }

    public static SharedPreferences share(Context context) {
        if (sharedPreferences == null) {
            sharedPreferences = context.getSharedPreferences("date", Context.MODE_PRIVATE);
        }
        return sharedPreferences;
    }

    public static Object getValue(Context context, String name) {
        return share(context).getString(name, "");
    }

    public static boolean setValue(Context context, String name, String value) {
        SharedPreferences.Editor e = share(context).edit();
        e.putString(name, value);
        Boolean bool = e.commit();
        return bool;
    }

    public static boolean setValue(Context context, String name, Integer value) {
        SharedPreferences.Editor e = share(context).edit();
        e.putInt(name, value);
        Boolean bool = e.commit();
        return bool;
    }
}
